//
//  AdCell.m
//  practise3
//
//  Created by 千锋 on 15/11/28.
//  Copyright (c) 2015年 abc. All rights reserved.
//

#import "AdCell.h"

@implementation AdCell
{
    UIScrollView * _scrollView;//滚动视图
    UIView * _backgroundView;//背景图片
    UILabel * _imageNameLabel;//图片名称
    UIPageControl * _pageControl;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


//重写初始化方法
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self==[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self creatUI];
    }
    return self;
}

//创建UI
-(void)creatUI
{
    //获取CELL的Size
    CGSize cellSize=self.frame.size;
    _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, cellSize.width, cellSize.height)];
    //添加到cell中，自定义cell需要将子视图添加到contentView中
    [self.contentView addSubview:_scrollView];
    //按页显示
    _scrollView.pagingEnabled=YES;
    //关闭弹簧效果
    _scrollView.bounces=NO;
   // _scrollView.backgroundColor=[UIColor redColor];
}
//重写imageNameArray的setter方法
-(void)setImageNameArray:(NSArray *)imageNameArray
{
    //给变量赋值
    _imageNameArray=imageNameArray;
    //在UIScrollView中添加图片
    CGFloat imageWide=CGRectGetWidth(_scrollView.frame);
    CGFloat imageHigth=CGRectGetHeight(_scrollView.frame);
    
    for (int index=0; index<self.imageNameArray.count; index++) {
        UIImageView * imageView=[[UIImageView alloc]initWithFrame:CGRectMake(index*imageWide, 0, imageWide, imageHigth)];
        imageView.image=[UIImage imageNamed:_imageNameArray[index]];
        [_scrollView addSubview:imageView];
    }
    //设置contentSize
    _scrollView.contentSize=CGSizeMake(imageWide*self.imageNameArray.count, imageHigth) ;
}

//-----------当父视图的位子和尺寸发生改变时，将回调次方法通知当前视图去更新子视图的尺寸和位子
//可能多次回调
-(void)layoutSubviews
{
    //更新子视图的位子和尺寸
    _scrollView.frame=self.frame;
    CGFloat imageWide=CGRectGetWidth(_scrollView.frame);
    CGFloat imageHight=CGRectGetHeight(_scrollView.frame);
    for (int index=0; index<_scrollView.subviews.count; index++) {
        UIView * view=_scrollView.subviews[index];
        view.frame=CGRectMake(index*imageWide, 0, imageWide, imageHight);
    }
    //更新ScrollView的ContentSize
    _scrollView.contentSize=CGSizeMake(imageWide*_imageNameArray.count, imageHight);
}
@end




























